//
//  RDiscoverTableViewCell.m
//  iMoive
//
//  Created by 侯伟 on 2021/8/25.
//

#import "RDiscoverTableViewCell.h"
#import "RVideoObj.h"
#import <SDWebImage/UIImageView+WebCache.h>

#import "RVideoDetailsViewController.h"

@interface RDiscoverTableViewCell (){
    MyRelativeLayout* _rankLayout;
    UILabel* _rankLb;
    UIImageView* _coverImage;
    UILabel* _descLb;
    UILabel* _nameLb;
    UILabel* _typeLb;
    UILabel* _performerLb;
    UIButton* _playBtn;
    UILabel* _countLb;
}
/// 显示内容视图
@property(nonatomic,weak) MyLinearLayout* contentLayout;

@end

@implementation RDiscoverTableViewCell


- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if(self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]){
        self.backgroundColor = UIColor.whiteColor;
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        
        [self initSubViews];
    }
    return self;
}


- (void)initSubViews{
    
    MyLinearLayout *contentLayout = [MyLinearLayout linearLayoutWithOrientation:(MyOrientation_Horz)];
    
    contentLayout.padding = UIEdgeInsetsMake(8, 16, 8, 16);
    contentLayout.myMargin = 0;
    contentLayout.myHeight = MyLayoutSize.wrap;
    contentLayout.subviewHSpace = 10.0;
    [self.contentView addSubview:contentLayout];
    _contentLayout = contentLayout;
    
    MyRelativeLayout* rankLayout = [MyRelativeLayout new];
    rankLayout.myHorzMargin = 0;
    rankLayout.myTop = 0;
    rankLayout.mySize = CGSizeMake(PX(24), PX(24));
    [contentLayout addSubview:rankLayout];
    _rankLayout = rankLayout;
    
    
    UILabel* rankLb = [UILabel lableWithFont:FONT_FIT(15) color:COLOR_WHITE text:@"1"];
    rankLb.textAlignment = NSTextAlignmentCenter;
    rankLb.myCenterX = rankLb.myCenterY = 0;
    [rankLayout addSubview:rankLb];
    _rankLb = rankLb;
    
    MyRelativeLayout* converLayout = [MyRelativeLayout new];
    converLayout.backgroundColor = COLOR_BACKGEOUND;
    converLayout.mySize = CGSizeMake(PX(105), PX(157));
    converLayout.myMargin = 0;
    converLayout.layer.cornerRadius = PX(5);
    converLayout.layer.masksToBounds = YES;
    [contentLayout addSubview:converLayout];
    
    UIImageView* coverImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"common_placeholder"]];
    coverImage.contentMode = UIViewContentModeScaleAspectFill;
    coverImage.mySize = CGSizeMake(PX(105), PX(157));
    coverImage.myMargin = 0;
    [converLayout addSubview:coverImage];
    _coverImage = coverImage;
    
    UILabel* descLb = [UILabel lableWithFont:FONT_FIT(12) color:COLOR_WHITE text:@""];
    descLb.backgroundColor = HEXACOLOR(0x0000008F);
    descLb.textAlignment = NSTextAlignmentRight;
    descLb.bottomPos.equalTo(coverImage.bottomPos);
    descLb.myHorzMargin = 0;
    descLb.myHeight = PX(20);
    [converLayout addSubview:descLb];
    _descLb = descLb;
    
    MyRelativeLayout *infoLayout = [MyRelativeLayout new];
    infoLayout.padding = UIEdgeInsetsMake(8.0, 0, 8.0, 0);
    infoLayout.myMargin = 0;
    infoLayout.weight = 1.0;
    [contentLayout addSubview:infoLayout];
    
    UILabel* nameLb = [UILabel lableWithFont:FONT_FIT(15) color:COLOR_BLACK_DARK text:@"影片名称"];
    nameLb.myHeight = MyLayoutSize.wrap;
    nameLb.myHorzMargin = 0;
    [infoLayout addSubview:nameLb];
    _nameLb = nameLb;
    
    UILabel* typeLb = [UILabel lableWithFont:FONT_FIT(12) color:COLOR_GRAY_BASE text:@"影片名称"];
    typeLb.myHorzMargin = 0;
    typeLb.topPos.equalTo(nameLb.bottomPos).offset(8.0f);
    [infoLayout addSubview:typeLb];
    _typeLb = typeLb;
    
    UILabel* performerLb = [UILabel lableWithFont:FONT_FIT(12) color:COLOR_GRAY_BASE text:@"演员表"];
    performerLb.myHorzMargin = 0;
    performerLb.numberOfLines = 2;
    performerLb.myHeight = MyLayoutSize.wrap;
    performerLb.topPos.equalTo(typeLb.bottomPos).offset(8.0f);
    [infoLayout addSubview:performerLb];
    _performerLb = performerLb;
    
    
    MyLinearLayout *btnLayout = [MyLinearLayout linearLayoutWithOrientation:(MyOrientation_Horz)];
    btnLayout.myBottom = btnLayout.myHorzMargin = 0;
    btnLayout.myHeight = MyLayoutSize.wrap;
    btnLayout.gravity = MyGravity_Horz_Between;
    [infoLayout addSubview:btnLayout];
    
    UIButton* playBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
    playBtn.userInteractionEnabled = NO;
    playBtn.backgroundColor = COLOR_THEME;
    [playBtn setTitle:@"立即播放" forState:UIControlStateNormal];
    [playBtn setTitleColor:COLOR_WHITE forState:UIControlStateNormal];
    playBtn.titleLabel.font = FONT_FIT(12);
//    [playBtn addTarget:self action:@selector(playMovie:) forControlEvents:UIControlEventTouchUpInside];
    playBtn.myVertMargin = 0;
    playBtn.mySize = CGSizeMake(PX(70), PX(24));
    playBtn.layer.cornerRadius = PX(12);
    playBtn.layer.masksToBounds = YES;
    [btnLayout addSubview:playBtn];
    _playBtn = playBtn;
    
//    UILabel* countLb = [UILabel lableWithFont:FONT_FIT(12) color:COLOR_GRAY_BASE text:@"0次播放"];
//    countLb.myCenterY = 0;
//    [btnLayout addSubview:countLb];
//    _countLb = countLb;
}

- (void)setIndex:(NSInteger)index{
    NSString* imageName;
    switch (index) {
        case 0:
            imageName = @"discover_rank_1";
            break;
        case 1:
            imageName = @"discover_rank_2";
            break;
        case 2:
            imageName = @"discover_rank_3";
            break;
        default:
            imageName = @"discover_rank_other";
            break;
    }
    if(imageName && index < 10){
        _rankLb.text = [NSString stringWithFormat:@"%ld",index+1];
        [_rankLb sizeToFit];
        
        _rankLayout.backgroundImage = [UIImage imageNamed:imageName];
    }else{
        _rankLayout.backgroundImage = NULL;
        _rankLb.text = NULL;
    }
    _rankLb.visibility = imageName?MyVisibility_Visible:MyVisibility_Gone;
}


- (void)setMovie:(RVideoObj *)movie{
    _movie = movie;
    
    [_coverImage sd_setImageWithURL:[NSURL URLWithString:movie.img] placeholderImage:[UIImage imageNamed:@"common_placeholder"] completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
        if(error){
            [[SDWebImageManager sharedManager] removeFailedURL:imageURL];
        }
    }];
    
    //信息
    _descLb.text = movie.info;
    [_descLb sizeToFit];
    
    //名称
    _nameLb.text = movie.name;
    [_nameLb sizeToFit];
    
    //类型，年代
    _typeLb.text = [NSString stringWithFormat:@"%@，%@",movie.channel2_id,movie.year];
    [_typeLb sizeToFit];
    
    //演员
    _performerLb.text = movie.actors;
    [_performerLb sizeToFit];
    
//    //播放次数
//    _countLb.text = [NSString stringWithFormat:@"播放%u次 ",arc4random()%1000];
//    [_countLb sizeToFit];
}

//- (void)playMovie:(UIButton*)sender{
//    RVideoDetailsViewController* detailController = [RVideoDetailsViewController new];
//    detailController.movie = self.movie;
//    [UIViewController showViewController:detailController animated:YES];
//}


#pragma private -- Size

- (CGSize)systemLayoutSizeFittingSize:(CGSize)targetSize withHorizontalFittingPriority:(UILayoutPriority)horizontalFittingPriority verticalFittingPriority:(UILayoutPriority)verticalFittingPriority{
    
    CGSize cellSize = CGSizeZero;
    if (@available(iOS 11.0, *)) {
        cellSize = [self.contentLayout sizeThatFits:CGSizeMake(targetSize.width - self.safeAreaInsets.left - self.safeAreaInsets.right, targetSize.height)];
    } else {
        CGSize size = targetSize;
        size.width = size.width;
        cellSize = [self.contentLayout sizeThatFits:size]; //如果使用系统自带的分割线，请记得将返回的高度height+1
    }
    return cellSize;
}
@end
